from django.contrib.auth.models import User
from django.db import models


class BaseModel(models.Model):
    STATUS_CHOICES = (
        ('0', '无效'),
        ('1', '有效'),
    )
    id = models.AutoField(primary_key=True)
    status = models.CharField(verbose_name='状态', choices=STATUS_CHOICES, default='1', max_length=200)
    created_user = models.ForeignKey(User, null=True,help_text="创建人", verbose_name="创建人", on_delete=models.CASCADE,blank=True,editable=False)
    created_time = models.DateTimeField(auto_now_add=True, help_text="创建时间", verbose_name='创建时间')
    last_mod_time = models.DateTimeField(auto_now=True, help_text="修改时间", verbose_name='修改时间')


# Create your models
class Sysyems(BaseModel):
    system_code = models.CharField(verbose_name="系统编码", max_length=200, blank=True)
    system_name = models.CharField(verbose_name="系统名称", max_length=200, blank=True)
    system_manager = models.CharField(verbose_name="系统负责人", max_length=200, blank=True)
    dev_manager = models.ManyToManyField(User,help_text="开发人员", verbose_name="开发人员",blank=True)
    test_manager =models.CharField(max_length=200,help_text="测试人员", verbose_name="测试人员",blank=True)
    op_manager = models.CharField(max_length=200,help_text="运维人员", verbose_name="运维人员",blank=True)
    pro_manager = models.CharField(max_length=200,help_text="产品人员", verbose_name="产品人员",blank=True)

    class Meta:
        db_table = 'org_system'
        verbose_name = '系统信息'
        verbose_name_plural = '系统信息'

    def __str__(self):
        return self.system_name


class Environment(BaseModel):
    ENV_TYPE_CHOICES = (('mit', 'mit'), ('sit', 'sit'), ('uat', 'uat'),)
    system_name = models.ForeignKey(Sysyems, verbose_name="所属系统", help_text="所属系统", on_delete=models.CASCADE)
    entype = models.CharField(choices=ENV_TYPE_CHOICES, max_length=200, verbose_name="环境类型", default='online',
                              help_text="环境类型", blank=True)
    address = models.CharField(verbose_name="环境地址", max_length=800, help_text="环境地址", blank=True)
    ip = models.CharField(verbose_name="IP地址", max_length=800, help_text="IP地址", blank=True)
    port = models.CharField(verbose_name="端口号", max_length=800, help_text="IP地址", blank=True)
    login_account = models.CharField(verbose_name="登录账号", max_length=800, help_text="登录账号", blank=True)
    shell_account = models.CharField(verbose_name="服务器账号", max_length=800, help_text="服务器账号", blank=True)


    class Meta:
        db_table = 'org_environment'
        verbose_name = '环境信息'
        verbose_name_plural = '环境信息'

    def __str__(self):
        return self.system_name.system_name + self.entype


class DbInfo(BaseModel):
    ENV_TYPE_CHOICES = (('mit', 'mit'), ('sit', 'sit'), ('uat', 'uat'),)
    DB_TYPE =(('django.db.backends.oracle','oracel'),('django.db.backends.mysql','mysql'))
    system_name = models.ForeignKey(Sysyems, verbose_name="所属系统", help_text="所属系统", on_delete=models.CASCADE)
    entype = models.CharField(choices=ENV_TYPE_CHOICES, max_length=200, verbose_name="环境类型", default='online',
                              help_text="环境类型", blank=True)
    db_engine = models.CharField(verbose_name="数据库类型",choices=DB_TYPE,max_length=800,help_text="数据库类型",blank=True)
    db_address = models.CharField(verbose_name="数据库地址", max_length=800, help_text="数据库地址", blank=True)
    db_instance = models.CharField(verbose_name="数据库实例", max_length=800, help_text="数据库实例", blank=True)
    db_user = models.CharField(verbose_name="数据库账号", max_length=300,help_text="数据库密码",blank=True)
    db_password = models.CharField(verbose_name="数据库密码", max_length=300,help_text="数据库密码",blank=True)
    db_port = models.CharField(verbose_name="数据库端口号",max_length=300,help_text="数据库端口",blank=True)
    db_name = models.CharField(verbose_name="名称", max_length=800, help_text="项目名称", blank=True)

    class Meta:
        db_table = 'db_info'
        verbose_name = '数据库信息'
        verbose_name_plural = '数据库信息'


    def __str__(self):
        return self.system_name.system_name + self.db_name


class InterfaceInfo(BaseModel):
    METHOD_CHOICES = (('GET', 'GET'), ('POST', 'POST'), ('PUT', 'PUT'), ('DELETE', 'DELETE'), ('PATCH', 'PATCH'))
    system_name = models.ForeignKey(Sysyems, verbose_name="所属系统", help_text="所属系统", on_delete=models.CASCADE)
    description = models.CharField(max_length=100, verbose_name="接口描述", help_text="接口描述", )
    path = models.CharField(max_length=50, verbose_name="接口路径", blank=True)
    method = models.CharField(max_length=20, choices=METHOD_CHOICES, verbose_name="请求方法", blank=True)
    request_header = models.TextField(verbose_name="请求头", blank=True)

    request_body = models.TextField(verbose_name="请求体", blank=True)
    response_header = models.TextField(verbose_name="返回头", blank=True)
    response_body = models.TextField(verbose_name="返回体", blank=True)

    class Meta:
        db_table = 'org_interface_info'
        verbose_name = '接口信息'
        verbose_name_plural = '接口信息'

    def __str__(self):
        return self.description


class InterfaceHead(BaseModel):

    interface = models.ForeignKey(InterfaceInfo, on_delete=models.CASCADE, verbose_name="所属接口", related_name='headers')
    name = models.CharField(max_length=1024, verbose_name="标签")
    value = models.CharField(max_length=1024, blank=True, null=True, verbose_name='内容')

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '请求头'
        verbose_name_plural = '请求头管理'